package utils;

import com.sun.jndi.toolkit.url.UrlUtil;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.apache.http.client.utils.URIUtils;
import org.apache.poi.common.usermodel.HyperlinkType;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
import org.apache.poi.hssf.usermodel.HSSFPatriarch;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Hyperlink;
import org.apache.tomcat.util.buf.UriUtil;

public class ExcelUtils {

  public static void main(String[] args) {
    FileOutputStream fileOut = null;
    BufferedImage bufferImg = null;//图片
    try {
      // 先把读进来的图片放到一个ByteArrayOutputStream中，以便产生ByteArray
      ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
      //将图片读到BufferedImage
      bufferImg = ImageIO.read(new File("D:\\document\\workspace\\temp\\1.png"));
      // 将图片写入流中
      ImageIO.write(bufferImg, "png", byteArrayOut);
      // 创建一个工作薄
      HSSFWorkbook wb = new HSSFWorkbook();
      //创建一个sheet
      HSSFSheet sheet = wb.createSheet("out put excel");
      sheet.setColumnWidth(10, 40 * 256);
      sheet.setDefaultRowHeight((short) (150 * 20));
      HSSFRow row = sheet.createRow(0);
      HSSFCell cell = row.createCell(0);
      cell.setCellValue("第一行");

      HSSFRow row2 = sheet.createRow(1);
      HSSFCell cell2 = row2.createCell(0);
      cell2.setCellValue("第二行");

      // 利用HSSFPatriarch将图片写入EXCEL
      HSSFPatriarch patriarch = sheet.createDrawingPatriarch();

      /**
       * 该构造函数有8个参数
       * 前四个参数是控制图片在单元格的位置，分别是图片距离单元格left，top，right，bottom的像素距离
       * 后四个参数，前两个表示图片左上角所在的cellNum和 rowNum，后两个参数对应的表示图片右下角所在的cellNum和 rowNum，
       * excel中的cellNum和rowNum的index都是从0开始的
       *
       */
      //图片一导出到单元格B2中
      HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0,
          (short) 10, 0, (short) 11, 1);

      HSSFClientAnchor anchor2 = new HSSFClientAnchor(0, 0, 0, 0,
          (short) 10, 1, (short) 11, 2);
      // 插入图片
      patriarch.createPicture(anchor, wb.addPicture(byteArrayOut
          .toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));
      patriarch.createPicture(anchor2, wb.addPicture(byteArrayOut
          .toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));

      Hyperlink href = wb.getCreationHelper().createHyperlink(HyperlinkType.URL);
      href.setAddress("http://crm-api.fansfinancial.com/fileserver/group1/M00/00/43/CjQKj13os_WAO723AAd15eEJRoE654.png");
      cell.setHyperlink(href);
      //生成的excel文件地址
      fileOut = new FileOutputStream("D:\\document\\workspace\\temp\\123.xls");
      // 写入excel文件
      wb.write(fileOut);
    } catch (IOException io) {
      io.printStackTrace();
      System.out.println("io erorr : " + io.getMessage());
    } finally {
      if (fileOut != null) {
        try {
          fileOut.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
  }
}